<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/1
 * Time: 9:33
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\team\model\Comment as App;
class Console extends Common
{
    public function console()
    {
        //查询平台用户数
        $user_all = Db::name('user')->where('is_del',0)->count();
        //查询景点商户数
        $tour_all = Db::name('tours')->where('is_del',0)->where('is_pass',1)->count();
        //查询酒店商户数
        $hotel_all = Db::name('hotel')->where('is_del',0)->where('is_pass',1)->count();
        //查询美食商户数
        $food_all = Db::name('food_store')->where('is_del',0)->where('is_pass',1)->count();
        //旅行社商户数
        $team_all = Db::name('team')->where('is_del',0)->where('is_pass',1)->count();
        $this->assign('user_num',$user_all);
        $this->assign('tour_num',$tour_all);
        $this->assign('hotel_num',$hotel_all);
        $this->assign('food_num',$food_all);
        $this->assign('team_num',$team_all);

        //入住申请
        $tour_pass = Db::name('food_store')->where('is_pass',0)->where('is_del',0)->count();
        $hotel_pass = Db::name('hotel')->where('is_pass',0)->where('is_del',0)->count();
        $team_pass = Db::name('team')->where('is_pass',0)->where('is_del',0)->count();
        $count_pass = $tour_pass + $hotel_pass + $team_pass;
        $this->assign('count_pass',$count_pass);

        //待审核的帖子
        $method_pass = Db::name('method')->where('is_pass',0)->where('is_del','0')->count();
        $lv_pass = Db::name('friends_circle')->where('is_pass',0)->where('is_del',0)->count();
        $count_lv_pass = $method_pass+$lv_pass;
        $this->assign('count_lv_pass',$count_lv_pass);

        //待处理投诉
        $ts_pass = Db::name('order_complains')->where('complain_status',0)->count();
        $this->assign('ts_pass',$ts_pass);


        //获取待核销订单数
        $no_order = Db::name('tour_order')->where('is_del', 0)->where('tour_id',Session::get('tour_id'))->where('order_status', 1)->count();
        $this->assign('no_order', $no_order);
        //最近12天的订单业务总额
        $week = $this->get_weeks('');
        $weeks = json_encode($week[0]);
        $zong = json_encode($week[1]);
        $this->assign('week', $weeks);
        $this->assign('zong', $zong);
        //最近12天的订单业务总额
        $week_yes = $this->get_weeks_yes('');
        $heng = json_encode($week_yes[0]);
        $zong1 = json_encode($week_yes[1]);
        $this->assign('heng', $heng);
        $this->assign('zong1', $zong1);
        //获取订单总额
        $where = 'order_status=1 or order_status=3 or order_status=4';
        $tour_all_money = Db::name('tour_order')->where('is_del',0)->where($where)->sum('order_price');
        $hotel_all_money = Db::name('hotel_order')->where('is_del',0)->where($where)->sum('order_price');
        $team_all_money = Db::name('gtour_order')->where('is_del',0)->where($where)->sum('order_price');

        $all_money = $tour_all_money+$hotel_all_money+$team_all_money;
        $this->assign('all_money',$all_money);
        //获取今日的数据
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));

        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $where1['create_time'] = ['between time', [$beginToday,$endToday]];
        $tour_today_money = Db::name('tour_order')->where($where1)->where('is_del',0)->where($where)->sum('order_price');
        $hotel_today_money = Db::name('hotel_order')->where($where1)->where('is_del',0)->where($where)->sum('order_price');
        $team_today_money = Db::name('gtour_order')->where($where1)->where('is_del',0)->where($where)->sum('order_price');
        $today_money = $tour_today_money+$hotel_today_money+$team_today_money;
        $this->assign('today_money',$today_money);

        //php获取本月起始时间戳和结束时间戳
        $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
        $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
        $where2['create_time'] = ['between time', [$beginThismonth,$endThismonth]];
        $tour_month_money = Db::name('tour_order')->where($where2)->where('is_del',0)->where($where)->sum('order_price');
        $hotel_month_money = Db::name('hotel_order')->where($where2)->where('is_del',0)->where($where)->sum('order_price');
        $team_month_money = Db::name('gtour_order')->where($where2)->where('is_del',0)->where($where)->sum('order_price');
        $month_money = $tour_month_money+$hotel_month_money+$team_month_money;
        $this->assign('month_money',$month_money);

        //实际收益额
        //总
        $tour_sum_fee = Db::name('tour_order')->where('is_del',0)->where($where)->sum('commission_fee');
        $hotel_sum_fee = Db::name('hotel_order')->where('is_del',0)->where($where)->sum('commission_fee');
        $team_sum_fee = Db::name('gtour_order')->where('is_del',0)->where($where)->sum('commission_fee');
        $sum_fee = $tour_sum_fee+$hotel_sum_fee+$team_sum_fee;
        $all = $all_money-$sum_fee;
        $this->assign('all',$all);
        //本日
        $tour_sum_today_fee = Db::name('tour_order')->where($where1)->where('is_del',0)->where($where)->sum('commission_fee');
        $hotel_sum_today_fee = Db::name('hotel_order')->where($where1)->where('is_del',0)->where($where)->sum('commission_fee');
        $team_sum_today_fee = Db::name('gtour_order')->where($where1)->where('is_del',0)->where($where)->sum('commission_fee');
        $sum_today_fee = $tour_sum_today_fee+$hotel_sum_today_fee+$team_sum_today_fee;
        $today_fee = $today_money-$sum_today_fee;
        $this->assign('today_fee',$today_fee);
        //本月
        $tour_month_fee =  Db::name('tour_order')->where($where2)->where('is_del',0)->where($where)->sum('commission_fee');
        $hotel_month_fee =  Db::name('hotel_order')->where($where2)->where('is_del',0)->where($where)->sum('commission_fee');
        $team_month_fee =  Db::name('gtour_order')->where($where2)->where('is_del',0)->where($where)->sum('commission_fee');
        $month_fee = $tour_month_fee+$hotel_month_fee+$team_month_fee;
        $month_read = $month_money-$month_fee;
        $this->assign('month_read',$month_read);
        return view();
    }
    //订单月
    public function order_month()
    {
        if(Request::instance()->isAjax()){
            return json(['week'=>$this->get_month()[0],'zong'=>$this->get_month()[1]]);
        }
    }
    //订单日
    public function order_week()
    {
        if(Request::instance()->isAjax()){
            return json(['week'=>$this->get_weeks()[0],'zong'=>$this->get_weeks()[1]]);
        }
    }

    //订单月
    public function order_month_yes()
    {
        if(Request::instance()->isAjax()){
            return json(['week'=>$this->get_month_yes()[0],'zong'=>$this->get_month_yes()[1]]);
        }
    }
    //订单日
    public function order_week_yes()
    {
        if(Request::instance()->isAjax()){
            return json(['week'=>$this->get_weeks_yes()[0],'zong'=>$this->get_weeks_yes()[1]]);
        }
    }
    /**
     * 获取最近七天所有日期
     */
    function get_weeks($time = '', $format='m-d'){
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        $money = [];
        $money1 = [];
        $money2 = [];
        $money3 = [];
        $where1 = "order_status=1 or order_status=3 or order_status=4";
        for ($i=1; $i<=12; $i++){
            $date[] = date($format ,strtotime( '+' . $i-12 .' days', $time));
            $firstday = date('Y-m-d 00:00:00', strtotime( '+' . $i-12 .' days', $time));
            $lastday = date('Y-m-d 23:59:59',strtotime( '+' . $i-12 .' days', $time));
            $first = strtotime($firstday);
            $last = strtotime($lastday);
            $where['create_time'] = ['between time',[$first,$last]];
            $money1[] = Db::name('tour_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money2[] = Db::name('hotel_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money3[] = Db::name('gtour_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money[] = $money1[$i-1]+$money2[$i-1]+$money3[$i-1];
        }
        $times = [$date,$money];
        return $times;
    }

    function get_weeks_yes($time = '', $format='m-d'){
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        $money = [];
        $money1 = [];
        $money2 = [];
        $money3 = [];
        $money_all = [];
        $fee = [];
        $fee1 = [];
        $fee2 = [];
        $fee3 = [];
        $where1 = "order_status=1 or order_status=3 or order_status=4";
        for ($i=1; $i<=12; $i++){
            $date[] = date($format ,strtotime( '+' . $i-12 .' days', $time));
            $firstday = date('Y-m-d 00:00:00', strtotime( '+' . $i-12 .' days', $time));
            $lastday = date('Y-m-d 23:59:59',strtotime( '+' . $i-12 .' days', $time));
            $first = strtotime($firstday);
            $last = strtotime($lastday);
            $where['create_time'] = ['between time',[$first,$last]];
            $money1[] = Db::name('tour_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money2[] = Db::name('hotel_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money3[] = Db::name('gtour_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money_all[] = $money1[$i-1]+$money2[$i-1]+$money3[$i-1];
            $fee1[] = Db::name('tour_order')->where('is_del',0)->where($where)->where($where1)->sum('commission_fee');
            $fee2[] = Db::name('hotel_order')->where('is_del',0)->where($where)->where($where1)->sum('commission_fee');
            $fee3[] = Db::name('gtour_order')->where('is_del',0)->where($where)->where($where1)->sum('commission_fee');
            $fee[] = $fee1[$i-1]+$fee2[$i-1]+$fee3[$i-1];
            $money[] = $money_all[$i-1]-$fee[$i-1];
        }
        $times = [$date,$money];
        return $times;
    }
    /*
     * 获取最近12个月的数据
     */
    function get_month()
    {
        $dates = [];
        $money = [];
        $money1 = [];
        $money2 = [];
        $money3 = [];
        $currentTime = time();
        $cyear = floor(date("Y",$currentTime));
        $cMonth = floor(date("m",$currentTime));
        $where1 = "order_status=1 or order_status=3 or order_status=4";
        for($i=0;$i<12;$i++) {
            $nMonth = $cMonth - $i;
            $cyear = $nMonth == 0 ? ($cyear - 1) : $cyear;
            $nMonth = $nMonth <= 0 ? 12 + $nMonth : $nMonth;
            $dates[] = $cyear .'.'. $nMonth;
            $date = $cyear . "-" . $nMonth . "-1";
            $firstday = date('Y-m-01 00:00:00', strtotime($date));
            $lastday = date('Y-m-t 23:59:59', strtotime($date));
            $first = strtotime($firstday);
            $last = strtotime($lastday);
            $where["create_time"] = ['between time',[$first,$last]];
            $money1[] = Db::name('tour_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money2[] = Db::name('hotel_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money3[] = Db::name('gtour_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money[] = $money1[$i]+$money2[$i]+$money3[$i];
        }
        $times = [array_reverse($dates),array_reverse($money)];
        return $times;
    }

    function get_month_yes()
    {
        $dates = [];
        $money = [];
        $money1 = [];
        $money2 = [];
        $money3 = [];
        $money_all = [];
        $fee = [];
        $fee1 = [];
        $fee2 = [];
        $fee3 = [];
        $currentTime = time();
        $cyear = floor(date("Y",$currentTime));
        $cMonth = floor(date("m",$currentTime));
        $where1 = "order_status=1 or order_status=3 or order_status=4";
        for($i=0;$i<12;$i++) {
            $nMonth = $cMonth - $i;
            $cyear = $nMonth == 0 ? ($cyear - 1) : $cyear;
            $nMonth = $nMonth <= 0 ? 12 + $nMonth : $nMonth;
            $dates[] = $cyear .'.'. $nMonth;
            $date = $cyear . "-" . $nMonth . "-1";
            $firstday = date('Y-m-01 00:00:00', strtotime($date));
            $lastday = date('Y-m-t 23:59:59', strtotime($date));
            $first = strtotime($firstday);
            $last = strtotime($lastday);
            $where["create_time"] = ['between time',[$first,$last]];
            $money1[] = Db::name('tour_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money2[] = Db::name('hotel_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money3[] = Db::name('gtour_order')->where('is_del',0)->where($where)->where($where1)->sum('order_price');
            $money_all[] = $money1[$i]+$money2[$i]+$money3[$i];
            $fee1[] = Db::name('tour_order')->where('is_del',0)->where($where)->where($where1)->sum('commission_fee');
            $fee2[] = Db::name('hotel_order')->where('is_del',0)->where($where)->where($where1)->sum('commission_fee');
            $fee3[] = Db::name('gtour_order')->where('is_del',0)->where($where)->where($where1)->sum('commission_fee');
            $fee[] = $fee1[$i]+$fee2[$i]+$fee3[$i];
            $money[] = $money_all[$i]-$fee[$i];

        }
        $times = [array_reverse($dates),array_reverse($money)];
        return $times;
    }
}